package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.vo.Page;
import com.gxuwz.medical.exception.DbException;

public class ChronicDao extends GenericDao<Chronicdis> {
	@Override
	protected Chronicdis handle(ResultSet rs) throws SQLException {
		try {
			Chronicdis chronic = new Chronicdis();
			chronic.setChroniccode(rs.getString("chroniccode"));
			chronic.setPinyincode(rs.getString("pinyincode"));
			chronic.setChronicname(rs.getString("chronicname"));
			return chronic;
		} catch (SQLException e) {

			throw new SQLException("结果集转为实例失败!", e);
		}
	}

	/**
	 * 分页
	 * 
	 * @param sql
	 * @param params
	 * @param page
	 * @param size
	 * @return
	 * @throws SQLException
	 */
	public Page queryOjectsByPage(String sql, Object[] params, int pageNo,
			int size) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Page page = null;
		List<Chronicdis> datas = new ArrayList<Chronicdis>();
		try {
			int start = (pageNo - 1) * size;
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement(sql + " limit " + start + "," + size);
			int index = 1;
			if (params != null) {
				for (Object param : params) {
					if (param instanceof String) {
						pstmt.setString(index++, (String) param);
					}
					if (param instanceof Integer) {
						pstmt.setInt(index++, (Integer) param);
					}
					if (param instanceof Float) {
						pstmt.setFloat(index++, (Float) param);
					}
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Chronicdis chronic = handle(rs);
				datas.add(chronic);
			}
			// 统计SQL对应记录数
			int total = count(sql, params);
			page = new Page(total, pageNo, size, datas);
			return page;
		} catch (SQLException e) {
			throw new SQLException("执行SQL[" + sql + "]失败", e);
		}

	}
	
	public List<Chronicdis>familyChronicdisList() throws DbException{
		try{
			String sql = "select * from t_chronic";
			Object[]params={};
			return super.queryOjects(sql, params);
		}catch(SQLException e){
			throw new DbException("查询失败");
		}
	}

}
